﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Testcraft
{
    class OperationRequest<T>
    {
        public enum OperationStatus : int
        {
            NotStarted,
            Running,
            Completed,
            Canceled
        }

        public T data;
        private int status;

        public OperationStatus Status
        {
            get { return (OperationStatus)this.status; }
        }

        public OperationRequest(T data)
        {
            this.data = data;
            this.status = (int)OperationStatus.NotStarted;
        }

        public bool SignalCancellation()
        {
            return (Interlocked.CompareExchange(
                ref this.status,
                (int)OperationStatus.Canceled,
                (int)OperationStatus.NotStarted) == (int)OperationStatus.NotStarted);
        }

        public bool SignalRunning()
        {
            return (Interlocked.CompareExchange(
                ref this.status,
                (int)OperationStatus.Running,
                (int)OperationStatus.NotStarted) == (int)OperationStatus.NotStarted);
        }

        public bool SignalCompleted()
        {
            return (Interlocked.CompareExchange(
                ref this.status,
                (int)OperationStatus.Completed,
                (int)OperationStatus.Running) == (int)OperationStatus.Running);
        }
    }
}
